/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import java.io.*;

/**
 *
 * @author abdala
 */
public class Message implements Serializable{
    private static final long serialVersionUID = 31400L;
    protected String login; 
    public Message(String login) { //login source
        this.login = login;
    }
    @Override
    public String toString() {
        return "Message from " + login;
    }   
    public String getlogin() {
        return login;
    }
    public void setlogin(String login) {
        this.login = login;
    }
        
    public static byte[] toArray(Message msg) throws IOException{
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
        objectOut.writeObject(msg);
        objectOut.close(); 
        
        byte[] flux = byteOut.toByteArray();
        
        //objectOut.close();
        byteOut.close();
        return flux; // retourne un tableau d'octet
    }
    
    public static Message fromArray(byte[] array) throws IOException, ClassNotFoundException{
        ByteArrayInputStream byteIn = new ByteArrayInputStream(array);
        ObjectInputStream objectIn = new ObjectInputStream(byteIn);
        
        Message msg;
        msg = (Message)objectIn.readObject();
        
        return msg; 
    }
    
}
